home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / access / nbtree / nbtcompare.c next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  2.3 KB  |  157 lines

  1. /*
  2.  *  btcompare.c -- Comparison functions for btree access method.
  3.  *
  4.  *    These functions are stored in pg_amproc.  For each operator class
  5.  *    defined on btrees, they compute
  6.  *
  7.  *        compare(a, b):
  8.  *            < 0 if a < b,
  9.  *            = 0 if a == b,
  10.  *            > 0 if a > b.
  11.  */
  12.  
  13. #include "tmp/c.h"
  14. #include "tmp/postgres.h"
  15. #include "utils/nabstime.h"
  16.  
  17. RcsId("$Header: /private/postgres/src/access/nbtree/RCS/nbtcompare.c,v 1.8 1992/08/18 17:45:40 mao Exp $");
  18.  
  19. int32
  20. btint2cmp(a, b)
  21.     int16 a, b;
  22. {
  23.     return ((int32) (a - b));
  24. }
  25.  
  26. int32
  27. btint4cmp(a, b)
  28.     int32 a, b;
  29. {
  30.     return (a - b);
  31. }
  32.  
  33. int32
  34. btint24cmp(a, b)
  35.     int16 a;
  36.     int32 b;
  37. {
  38.     return (((int32) a) - b);
  39. }
  40.  
  41. int32
  42. btint42cmp(a, b)
  43.     int32 a;
  44.     int16 b;
  45. {
  46.     return (a - ((int32) b));
  47. }
  48.  
  49. int32
  50. btfloat4cmp(a, b)
  51.     float32 a, b;
  52. {
  53.     if (a > b)
  54.     return (1);
  55.     else if (a == b)
  56.     return (0);
  57.     else
  58.     return (-1);
  59. }
  60.  
  61. int32
  62. btfloat8cmp(a, b)
  63.     float64 a, b;
  64. {
  65.     if (a > b)
  66.     return (1);
  67.     else if (a == b)
  68.     return (0);
  69.     else
  70.     return (-1);
  71. }
  72.  
  73. int32
  74. btoidcmp(a, b)
  75.     ObjectId a, b;
  76. {
  77.     if (a > b)
  78.     return (1);
  79.     else if (a == b)
  80.     return (0);
  81.     else
  82.     return (-1);
  83. }
  84.  
  85. int32
  86. btabstimecmp(a, b)
  87.     AbsoluteTime a, b;
  88. {
  89.     if (AbsoluteTimeIsBefore(a, b))
  90.     return (1);
  91.     else if (AbsoluteTimeIsBefore(b, a))
  92.     return (-1);
  93.     else
  94.     return (0);
  95. }
  96.  
  97. int32
  98. btcharcmp(a, b)
  99.     char a, b;
  100. {
  101.     return ((int32) (a - b));
  102. }
  103.  
  104. int32
  105. btchar16cmp(a, b)
  106.     Name a;
  107.     Name b;
  108. {
  109.     return (strncmp(a, b, 16));
  110. }
  111.  
  112. int32
  113. bttextcmp(a, b)
  114.     struct varlena *a, *b;
  115. {
  116.     char *ap, *bp;
  117.     int len;
  118.     int res;
  119.  
  120.     ap = VARDATA(a);
  121.     bp = VARDATA(b);
  122.  
  123.     /* len is the length of the shorter of the two strings */
  124.     if ((len = VARSIZE(a)) > VARSIZE(b))
  125.     len = VARSIZE(b);
  126.  
  127.     /* len includes the four bytes in which string length is stored */
  128.     len -= sizeof(VARSIZE(a));
  129.  
  130.     /*
  131.      *  If the two strings differ in the first len bytes, or if they're
  132.      *  the same in the first len bytes and they're both len bytes long,
  133.      *  we're done.
  134.      */
  135.  
  136.     res = 0;
  137.     if (len > 0) {
  138.     do {
  139.         res = (int) (*ap++ - *bp++);
  140.         len--;
  141.     } while (res == 0 && len != 0);
  142.     }
  143.  
  144.     if (res != 0 || VARSIZE(a) == VARSIZE(b))
  145.     return (res);
  146.  
  147.     /*
  148.      *  The two strings are the same in the first len bytes, and they
  149.      *  are of different lengths.
  150.      */
  151.  
  152.     if (VARSIZE(a) < VARSIZE(b))
  153.     return (-1);
  154.     else
  155.     return (1);
  156. }
  157.